import requests
import re
import urllib

url_name = []


def get_html(url):
    hr = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
                        "AppleWebKit/537.36 (KHTML, like Gecko) Chrome"
                        "/70.0.3538.102 Safari/537.36 Edge/18.18363"}
    html = requests.get(url, headers=hr).text
    url_content = re.compile(r'<a href="/detail-[0-9]+.html">')
    url_contents = re.findall(url_content, html)
    print(url_contents)
    print((url_contents[0]))
    for i in url_contents:
        url_reg = r'data-mp4="(.*?)"'
        url_it = re.findall(url_reg, i)
        if url_it:
            name_reg = re.compile(r'href="/detail-.{8}?.html">(.*?)</a>')
            name_it = re.findall(name_reg, i)
            for i, k in zip(name_it, url_it):
                url_name.append([i, k])
                print(i, k)
    a = 0
    for i in url_name:
        urllib.request.urlretrieve(i[1], r'.\%s.mp4' % i[0])
        print("Downloading :"+i[0])

if __name__ == '__main__':
    url = "http://www.budejie.com/video/"
    get_html(url)
